//index.js
//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    backgroundUrl: 'https://636c-cloud1-5gnxbga2f5eed8c0-1305796051.tcb.qcloud.la/image/background.jpg?sign=f25bf24b2a44f08377b3adc0efb064df&t=1620884055',
    userInfo: {
      avatarUrl: 'https://636c-cloud1-5gnxbga2f5eed8c0-1305796051.tcb.qcloud.la/image/unlogin.jpeg?sign=9731708a14bab5fcd47307c1a838bc6a&t=1620883343',
      nickName: '未登录,请登录查看',
    },  
    openid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const ui = wx.getStorageSync('userInfo')
    if(ui !== '')
    {
      this.setData({
        userInfo: ui,
        openid: ui.openid
      })
      app.globalData.openid = ui.openid
      app.globalData.userInfo = ui
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const ui = wx.getStorageSync('userInfo')
    if(ui !== '')
    {
      this.setData({
        userInfo: ui,
        openid: ui.openid
      })
      app.globalData.openid = ui.openid
      app.globalData.userInfo = ui
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserInfo: function () {
    const _this = this
    wx.cloud.callFunction({
      name: 'login',
      success:res=>{
        console.log("云函数调用成功")
        console.log(res)
        _this.setData({
          openid: res.result.openid
        })
      }
    })
    wx.getUserProfile({
      desc: '用于完善个人信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        _this.setData({
          userInfo: res.userInfo,
        })
        app.globalData.openid = this.data.openid
        this.data.userInfo.openid = this.data.openid
        app.globalData.userInfo = this.data.userInfo
        wx.setStorageSync('userInfo', _this.data.userInfo)
      }
    })
  },
  exit: function()
  {
    wx.clearStorage()
    wx.switchTab({
      url: '/pages/personpage/index',
    })
    const ui = wx.getStorageSync('userInfo')
    console.log(ui)
    const userInfo = {
      avatarUrl: 'https://636c-cloud1-5gnxbga2f5eed8c0-1305796051.tcb.qcloud.la/image/unlogin.jpeg?sign=9731708a14bab5fcd47307c1a838bc6a&t=1620883343',
      nickName: '未登录,请登录查看',
    }
    if(ui == '')
    {
      this.setData({
        userInfo: userInfo,
        openid: ''
      })
      wx.showToast({
        title: '退出登录成功',
      })
    }
  }
})